package com.cheer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.OaActivityPartiDao;
import com.cheer.entity.ActivityEntity;
import com.cheer.entity.OaActivityPartiEntity;
import com.cheer.entity.OaUserEntity;
import com.cheer.exception.ServiceException;
import com.cheer.param.ActivityCheckinParam;
import com.cheer.service.ActivityPartiService;
import com.cheer.service.OaLearnClassService;
import com.cheer.service.OaUserService;
import com.cheer.service.TencentMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chufangzheng
 */
@Service
@Slf4j
public class ActivityPartiServiceImpl extends ServiceImpl<OaActivityPartiDao, OaActivityPartiEntity> implements ActivityPartiService  {

    @Autowired
    private OaUserService oaUserService;

    @Autowired
    private OaLearnClassService oaLearnClassService;

    @Autowired
    private TencentMapService tencentMapService;


    @Override
    public Integer getApplyNum(Integer actId) {

        QueryWrapper<OaActivityPartiEntity> qw = new QueryWrapper();
        qw.lambda().eq(OaActivityPartiEntity::getActivityId, actId);
        return count(qw);
    }

    @Override
    public Boolean getIsRegistered(Integer actId, Integer userId) {

        QueryWrapper<OaActivityPartiEntity> qw = new QueryWrapper();
        qw.lambda().eq(OaActivityPartiEntity::getActivityId, actId)
                .eq(OaActivityPartiEntity::getUserId, userId);

        OaActivityPartiEntity one = getOne(qw);
        if( null == one){
            return false;
        }
        return one.getIsRegistered();
    }

    @Override
    public Boolean getIsCheckin(Integer actId, Integer userId) {

        QueryWrapper<OaActivityPartiEntity> qw = new QueryWrapper();
        qw.lambda().eq(OaActivityPartiEntity::getActivityId, actId)
                .eq(OaActivityPartiEntity::getUserId, userId);

        OaActivityPartiEntity one = getOne(qw);
        if( null == one){
            return false;
        }
        return one.getIsCheckedIn();
    }

    @Override
    public void registration(Integer actId, Integer userId, OaUserEntity oaUserEntity) {
        QueryWrapper<OaActivityPartiEntity> qw = new QueryWrapper();
        qw.lambda().eq(OaActivityPartiEntity::getActivityId, actId)
                .eq(OaActivityPartiEntity::getUserId, userId);
        OaActivityPartiEntity partiEntity = getOne(qw);
        if(partiEntity == null){
            // 创建一条记录
            partiEntity = new OaActivityPartiEntity();
            partiEntity.setChannel("系统");
            partiEntity.setName(oaUserEntity.getName());
            partiEntity.setActivityId(actId);
            partiEntity.setIsRegistered(true);
            partiEntity.setIsCheckedIn(false);
            partiEntity.setPhone(oaUserEntity.getPhone());
            partiEntity.setUserId(userId);
            partiEntity.setOrgPath(oaUserEntity.getPath());
            save(partiEntity);
        }else{
            throw new ServiceException("当前用户已在报名列表中请勿重复报名！");
        }
    }

    @Override
    public void checkin(ActivityCheckinParam checkInParam, Integer userId, ActivityEntity act) {
        // 签到
        QueryWrapper<OaActivityPartiEntity> qw = new QueryWrapper();
        qw.lambda().eq(OaActivityPartiEntity::getActivityId, checkInParam.getId())
                .eq(OaActivityPartiEntity::getUserId, userId);
        OaActivityPartiEntity partiEntity = getOne(qw);
        if(partiEntity == null){
            throw new ServiceException("当前用户未报名");
        }
        if(partiEntity.getIsCheckedIn()){
            throw new ServiceException("当前用户已经签到，请勿重复签到！");
        }
        // 检查当前距离多少米 tencentMapService
        String to = act.getLocationLatitude() + "," + act.getLocationLongitude();
        Double distance = tencentMapService.getLocation(checkInParam.getFromLocation(), to);
        log.info("当前距离为：" + distance);
        log.info("设定打卡距离为：" + act.getLocationRange());
        if( Double.valueOf(act.getLocationRange()) < distance ){
            throw new ServiceException("请在当前指定地点 范围进行打卡！");
        }
        // 改变签到状态
        partiEntity.setIsCheckedIn(true);
        // 存入当前秒数
        partiEntity.setCheckedTime(System.currentTimeMillis()/1000);
        // 存入经度 纬度
        partiEntity.setCheckedLocal(checkInParam.getFromLocation());
        updateById(partiEntity);
    }
}
